package com.qidianit.task.service.impl;

import com.qidianit.task.mapper.TaskMapper;
import com.qidianit.task.pojo.Task;
import com.qidianit.task.service.TaskService;
import com.qidianit.task.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author gfdycxy
 * @Company cxy
 * @Description
 * @Time 2024/9/11 11:18
 */
@Service
public class TaskServiceImpl implements TaskService {
    @Autowired
    private TaskMapper taskMapper;
    @Override
    public QdResult list(String projectId, Long parentId) {
        return QdResult.success(taskMapper.selectList(projectId,parentId));
    }

    @Override
    public QdResult save(Task task) {
        task.setTaskStatus(1);
        task.setTaskParent(2);
        task.setCreateTime(new Date());
        int i = taskMapper.insertTask(task);
        // 判断当前任务是否有父级任务
        if(i == 1 && !task.getTaskParentId().equals(new Long(0))){
            // 查询父级任务
            Task parentTask = taskMapper.selectById(task.getTaskParentId());
            // 判断父级任务的parent是否是1
            if(parentTask.getTaskParent().equals(new Integer(2))){
                parentTask.setTaskParent(1);
                taskMapper.updateById(parentTask);
            }
        }
        return i == 1?QdResult.success(task):QdResult.error();
    }

    @Override
    public QdResult selectByTaskId(String taskId) {
        return QdResult.success(taskMapper.selectByTaskId(taskId));
    }
    @Override
    public QdResult queryById(Long taskId) {
        return QdResult.success(taskMapper.selectById(taskId));
    }

    @Override
    public QdResult edit(Task task) {
        return taskMapper.updateById(task) == 1?QdResult.success():QdResult.error();
//        return QdResult.success( taskMapper.updateById(task));
    }
    @Override
    public QdResult deleteTask(Long taskId) {
        return QdResult.success(200,"OK",taskMapper.deleteTask(taskId));
    }

    @Override
    public QdResult deleteTaskByIds(Long[] ids) {
        return QdResult.success(200,"OK",taskMapper.deleteTaskByIds(ids));
    }

    @Override
    public QdResult count(Long taskId) {
        return QdResult.success(taskMapper.count(taskId));
    }
}